package com.zhongli.devplatform.utils.user;//package com.zhongli.devplatform.utils;

import com.zhongli.devplatform.bean.SecurityUser;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

public class UserUtil {

    public static SecurityUser getCurrUser() {
        Object a = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        SecurityUser securityUser = (SecurityUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return securityUser;
    }


    public static Boolean isLogin() {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return principal instanceof SecurityUser;
    }


    public static String getUserRole(){
        Collection<? extends GrantedAuthority> authorities =
                SecurityContextHolder.getContext().getAuthentication().getAuthorities();
        List<String> list = authorities.stream().map(GrantedAuthority::getAuthority).collect(Collectors.toList());
        String[] strings = list.get(0).split("=");
        return strings[1];
    }


}
